package com.whp.yky.yky.controller;



import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.whp.yky.yky.entity.poentity.User;
import com.whp.yky.yky.entity.voentity.VoToken;
import com.whp.yky.yky.entity.voentity.VoUser;
import com.whp.yky.yky.response.ResponseData;

import com.whp.yky.yky.service.UserService;
import com.whp.yky.yky.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "用户接口")
@CrossOrigin
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @ApiOperation(value="获取用户信息", notes="根据用户名获取用户全部信息")
    @PostMapping("/findAllByUserName")
    public Object getUserInfoByID(@RequestParam String username){

        if (username!=null&&!"".equals(username)){

            return userService.getUserInfoByUserName(username);

        }else{

            return "获取信息失败";

        }


    }

    @ApiOperation(value="新增用户", notes="新增用户")
    @PostMapping("/insertUser")
    public String insertUserInfo(@RequestBody Map<String,String> userMap){


        User user=new User();
        user.setId(userMap.get("id"));
        user.setUsername(userMap.get("username"));
        user.setPassword(userMap.get("password"));
        user.setOrg(userMap.get("org"));
        user.setType(userMap.get("type"));
        user.setAvatar(userMap.get("avatar"));


        boolean result= userService.insertUserInfo(user);
        if (result){
            return "success";
        }else {
            return "fail";
        }
    }

    @DeleteMapping("/deletUserByID/{id}")
    @ApiOperation(value="删除用户", notes="根据id删除用户")
    public String deletUserByID(@PathVariable("id") String id){
        boolean result = userService.deleteUserById(id);
        if (result){
            return "sucess";
        }else{
            return "false";
        }
    }

    @ApiOperation(value="修改用户信息", notes="修改用户信息")
    @PostMapping("/updateUserInfo")
    public  String updateUserInfoById(@RequestBody Map<String,String> userMap){

        User user =new User();
        user.setId(userMap.get("id"));
        user.setUsername(userMap.get("username"));
        user.setPassword(userMap.get("password"));
        user.setType(userMap.get("type"));
        user.setOrg(userMap.get("org")) ;
        user.setAvatar(userMap.get("avatar"));


        boolean result= userService.updateUserInfo(user);
        if (result){
            return "success";
        }else {
            return "fail";
        }
    }

    @ApiOperation(value="获取所有用户信息", notes="获取所有用户信息")
    @GetMapping("/userInfo")
    public ResponseData getUserInfo(@RequestParam String token){

        ResponseData res = new ResponseData();
        try {
            DecodedJWT verify = JwtUtil.verify(token);

        }catch (SignatureVerificationException e){
            e.printStackTrace();
        }catch (TokenExpiredException e){
            e.printStackTrace();
        }catch (AlgorithmMismatchException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }

        res.setCode(20000);
        res.setData(userService.getUserInfo());


        return res;
    }


    @ApiOperation(value="登录", notes="登录")
    @PostMapping("/login")
    public ResponseData login(@RequestBody VoUser user){


        Map<String,String> tk = new HashMap<>();
        tk.put("username",user.getUsername());
        tk.put("password",user.getPassword());
        ResponseData res = new ResponseData();
        try {
            VoUser vouser = userService.login(user);
            if (vouser!=null){
                String token = JwtUtil.getToken(tk);
                res.setCode(20000);
                res.setData(new VoToken(token));
            }else{
                res.setCode(500);
            }
        }catch (Exception e){
            e.printStackTrace();
        }




        return res;


    }

    @ApiOperation(value="登出", notes="登出")
    @PostMapping("/logout")
    public ResponseData logout(){



        ResponseData res = new ResponseData();


        res.setCode(20000);
        res.setData("sucess");


        return res;


        


    }




}
